﻿#include "freqw24item.h"

#include <QDebug>
#include <QGraphicsScene>

FreqW24Item::FreqW24Item(const QStringList& machineList, const QList<QStringList>& valueList, QObject *parent, QGraphicsItem *item) : QObject(parent), QGraphicsItem(item),
    m_machineList(machineList),
    m_valueList(valueList)
{
    //缓存在设备坐标系中，并且只有在位置、大小或外观发生变化时才需要重新计算。
    this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}

QRectF FreqW24Item::boundingRect() const
{
    return QRectF(0, 0, scene()->width(), 320);
}

void FreqW24Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
    painter->setCompositionMode(QPainter::CompositionMode_Source);

    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->setFont(QFont("Helvetica", 12));

    // 绘制网格线
    painter->drawLine(QLineF(30, 0, 870, 0));
    painter->drawLine(QLineF(30, 40, 870, 40));
    painter->drawLine(QLineF(30, 80, 870, 80));
    painter->drawLine(QLineF(30, 320, 870, 320));
    painter->drawLine(QLineF(130, 160 , 870, 160));
    painter->drawLine(QLineF(130, 240 , 870, 240));

    painter->drawLine(QLineF(30, 0, 30, 320));
    painter->drawLine(QLineF(80, 80, 80, 320));
    painter->drawLine(QLineF(130, 80, 130, 320));
    for (int i = 0; i < 5; i++)
        painter->drawLine(QLineF(260 + i * 120, 0, 260 + i * 120, 320));
    painter->drawLine(QLineF(870, 0, 870, 320));

    // 文字说明
    painter->drawText(QPointF(100, 25), QStringLiteral("项目或参数"));
    painter->drawText(QPointF(290, 25), QStringLiteral("I作动器"));
    painter->drawText(QPointF(410, 25), QStringLiteral("II作动器"));
    painter->drawText(QPointF(530, 25), QStringLiteral("III作动器"));
    painter->drawText(QPointF(650, 25), QStringLiteral("IV作动器"));
    painter->drawText(QPointF(770, 25), QStringLiteral("技术要求"));
    painter->drawText(QPointF(100, 65), QStringLiteral("角频率"));
    painter->drawText(QPointF(290, 65), QStringLiteral("相角(°)"));
    painter->drawText(QPointF(410, 65), QStringLiteral("相角(°)"));
    painter->drawText(QPointF(530, 65), QStringLiteral("相角(°)"));
    painter->drawText(QPointF(650, 65), QStringLiteral("相角(°)"));
    painter->drawText(QPointF(770, 65), QStringLiteral("相角(°)"));
    painter->drawText(QPointF(45, 115), QStringLiteral("频"));
    painter->drawText(QPointF(45, 140), QStringLiteral("率"));
    painter->drawText(QPointF(45, 165), QStringLiteral("特"));
    painter->drawText(QPointF(45, 190), QStringLiteral("性"));
    painter->drawText(QPointF(45, 215), QStringLiteral("4°"));
    painter->drawText(QPointF(45, 240), QStringLiteral("电"));
    painter->drawText(QPointF(45, 265), QStringLiteral("动"));
    painter->drawText(QPointF(45, 290), QStringLiteral("泵"));
    painter->drawText(QPointF(95, 165), QStringLiteral("位"));
    painter->drawText(QPointF(95, 190), QStringLiteral("移"));
    painter->drawText(QPointF(95, 215), QStringLiteral("表"));
    painter->drawText(QPointF(95, 240), QStringLiteral("决"));
    painter->drawText(QPointF(180, 125), QStringLiteral("6.28"));
    painter->drawText(QPointF(180, 205), QStringLiteral("20"));
    painter->drawText(QPointF(180, 285), QStringLiteral("60"));
    painter->drawText(QPointF(750, 125), QStringLiteral("≥-18.0"));
    painter->drawText(QPointF(750, 205), QStringLiteral("≥-50.0"));
    painter->drawText(QPointF(750, 285), QStringLiteral("实测"));

    // 文字数值: 1, 2, 3, 4
    if (m_machineList.contains("A1")) {
        painter->drawText(QPointF(290, 125), m_valueList.at(0).at(0));
        painter->drawText(QPointF(290, 205), m_valueList.at(1).at(0));
        painter->drawText(QPointF(290, 285), m_valueList.at(2).at(0));
    }

    if (m_machineList.contains("A2")) {
        painter->drawText(QPointF(410, 125), m_valueList.at(0).at(1));
        painter->drawText(QPointF(410, 205), m_valueList.at(1).at(1));
        painter->drawText(QPointF(410, 285), m_valueList.at(2).at(1));
    }

    if (m_machineList.contains("A3")) {
        painter->drawText(QPointF(530, 125), m_valueList.at(0).at(2));
        painter->drawText(QPointF(530, 205), m_valueList.at(1).at(2));
        painter->drawText(QPointF(530, 285), m_valueList.at(2).at(2));
    }

    if (m_machineList.contains("A4")) {
        painter->drawText(QPointF(650, 125), m_valueList.at(0).at(3));
        painter->drawText(QPointF(650, 205), m_valueList.at(1).at(3));
        painter->drawText(QPointF(650, 285), m_valueList.at(2).at(3));
    }
}
